#include <stdlib.h>
#include <stdio.h>
#include "cqueue.h"
#include <ctype.h>
#include <limits.h>
#include <string.h>
 

struct link_list *head_ptr = NULL;
struct link_list *tail_ptr = NULL;

 
 
 
 int main(){
    char line[50];
    char *opt;
    char *idnum;
    char *firstname;
    char *lastname;
    int id;
    int i = 0;
    FILE *datfile;
    int inbytes;

    
    

      while(i == 0){
        fgets(line,sizeof(line), stdin);

        opt = strtok(line," ");
       
        if(strcmp(opt,"a")==0 | strcmp(opt, "A")==0){
           
            idnum = strtok(NULL," ");
            
            if(!idnum)
                printf("ERROR1\n");
            else{
                 printf("idnum: %d\n",atoi(idnum));
            id = atoi(idnum);

            if(id < 0 | id > INT_MAX)
                printf("ERROR2\n");
            else if(addCust(id) == 0)
                printf("OK\n");

            else
               printf("ERROR3\n");
           }
        }

        else if(strcmp(opt, "r\n")==0 | strcmp(opt, "R\n")==0){
            if(custCount() == 0)
                printf("ERROR4\n");
            else
                printf("%d\n",remCust());
        }

        else if(strcmp(opt, "c\n")==0 | strcmp(opt, "C\n")==0){
            printf("%d\n",custCount());
        }

        else if(strcmp(opt, "e\n")==0 | strcmp(opt, "E\n")==0){
            saveData();
            return EXIT_FAILURE;
        }

        else{
            printf("ERROR5\n");
        }
    }
 }



 int addCust(int id){
     struct link_list *new_cust_ptr;
     new_cust_ptr = malloc(sizeof(struct link_list));
     if(head_ptr == NULL){
         head_ptr = new_cust_ptr;         
         (*head_ptr).id = id;
         (*head_ptr).prev_ptr = NULL;
         tail_ptr = head_ptr;
         printf("ID: %d\n", (*head_ptr).id);
         return 0;
     }
     else if(parseList(id) == 0){
         struct link_list *temp_ptr;
         printf("current head pointer is: %d\n",(*head_ptr).id);
         (*new_cust_ptr).id = id;
         (*tail_ptr).next_ptr = new_cust_ptr;
         temp_ptr = tail_ptr;
         tail_ptr = new_cust_ptr;              
         (*tail_ptr).prev_ptr = temp_ptr;
         return 0;
     }
     else{
         printf("testing id %d result: %d\n",id,parseList(id));
         return -1;
     }
 }

 int remCust(){
     int id;
     struct link_list *temp_ptr;
     printf("this sucks\n");
     id = (*head_ptr).id;
     temp_ptr = head_ptr;
     head_ptr = (*head_ptr).next_ptr;

     if(head_ptr == tail_ptr){
         head_ptr == NULL;
         tail_ptr == NULL;
         return id;
     }
     free(temp_ptr);
     return id;
 }

 int custCount(){
     int num = 0;
     struct link_list *temp_ptr;
     temp_ptr = head_ptr;

     while(temp_ptr != NULL){
         printf("id: %d\n",(*temp_ptr).id);
         temp_ptr = (*temp_ptr).next_ptr;
         ++num;
     }
     return num;
 }

 int parseList(int id){
     
     struct link_list *temp_ptr;
     temp_ptr = head_ptr;
     while(temp_ptr != NULL){
         if((*temp_ptr).id == id){
             return -1;
         }
         temp_ptr = (*temp_ptr).next_ptr; 
     }
     return 0;
 }

 int saveData(){
     FILE *outFile;
     if((outFile = fopen("cqueue.dat","wb")) == NULL)
         printf("Error writing to file");
     else{
         fwrite(head_ptr,1,(sizeof(struct link_list)),outFile);
         fclose(outFile);
     }
     return 0;
 }

 int loadData(){
     

     return 0;
 }
